function REPORT_MSG(op, data = {}, rid = "", status = "done") {
  return fetch("/__wx_channels_api/report", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      op,
      data,
      rid,
      status,
    }),
  });
}

function get_run(keyword, title) {
    const query = new URLSearchParams({
        search_channel: 'aweme_general',
        enable_history: 1,
        keyword,
        search_source: 'switch_tab'
    });
    fetch('https://www.douyin.com/aweme/v1/web/general/search/single/')
}

window.document.addEventListener("DOMContentLoaded", function () {
  setInterval(() => {
    REPORT_MSG("START_REQUEST")
      .then((res) => {
        res
          .json()
          .then((json) => {
            // 搜索
            if (json.op === "/search") {
              const id = json.rid;

              const { name, title } = json.data;
              REPORT_MSG("inject 正在抓取数据", json);
              get_run(name, title)
                .then((data) => {
                  REPORT_MSG(`SEND_RESPONSE`, data, id);
                })
                .catch((e) => {
                  REPORT_MSG("SEND_RESPONSE", e.message, id);
                });
            }
          })
          .catch((e) => {
            REPORT_MSG("SEND_RESPONSE", e.message, id);
          });
      })
      .catch((e) => {
        REPORT_MSG("请求异常:", e.message);
      });
  }, 100);
});
